/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hadss_intents/hadss_intents.dart';

void main() {
  const areaMethodChannel = MethodChannel('hadss_intents');

  TestWidgetsFlutterBinding.ensureInitialized();

  setUp(() {
    // 模拟 MethodChannel 调用
    TestDefaultBinaryMessengerBinding.instance?.defaultBinaryMessenger
        .setMockMethodCallHandler(
      areaMethodChannel,
      (MethodCall methodCall) async {
        if (methodCall.method == 'shareIntent') {
          expect(methodCall.arguments['shareList'], isNotNull);
          return Future(() => null);
        } else if (methodCall.method == 'deleteIntent') {
          expect(methodCall.method, 'deleteIntent');
          expect(methodCall.arguments['intentName'], isNotNull);
          return Future(() => null);
        } else if (methodCall.method == 'deleteEntity') {
          expect(methodCall.method, 'deleteEntity');
          expect(methodCall.arguments['entityName'], isNotNull);
          expect(methodCall.arguments['entityIds'], isNotNull);
          return Future(() => null);
        } else if (methodCall.method == 'getSide') {
          expect(methodCall.method, 'getSide');
          expect(methodCall.arguments['renew'], true);
          return Future(() => 'getSide');
        }
        return null;
      },
    );
  });

  tearDown(() {
    TestDefaultBinaryMessengerBinding.instance?.defaultBinaryMessenger
        .setMockMethodCallHandler(
      areaMethodChannel,
      null,
    );
  });

  // 测试 shareIntent 方法
  test('shareIntent test', () {
    List<InsightIntent> list = [];
    InsightIntent intent = InsightIntent();
    intent.identifier = "86d23b0-6520-5674-81e5-2136b5";
    intent.intentVersion = "1.0";
    intent.intentName = "PlayVideo";
    intent.intentActionInfo = {
      "actionMode": "EXPECTED",
      "currentSerialNumber": 1,
      "currentPercentage": 50
    };
    intent.intentEntityInfo = {
      "entityName": "Video",
      "entityId": "12949589",
      "entityGroupId": "129495",
      "displayName": "flutter测试video",
      "description":
          "该剧讲述了上世纪三十年代，爱国青年魏若来目睹社会黑暗，逐步找到人生方向，最终投身到中国共产党领导的新民主主义革命历史洪流的过程。",
      "logoURL":
          "https://consumer.huawei.com/etc/designs/huawei-cbg-site/clientlib-campaign-v4/common-v4/images/logo.svg",
      "keywords": ["视频"],
      "artists": ["殷桃", "王骁", "章若楠", "秦俊杰", "张哲华", "陈昊宇"],
      "category": "电视剧",
      "tags": ["都市", "家庭"],
      "playType": "正片",
      "score": 7.4,
      "rankingHint": 99,
      "videoName": "测试video",
      "videoImage":
          "https://consumer.huawei.com/content/dam/huawei-cbg…/cn/mkt/mobileservices/video/230912/img01-mob.jpg"
    };
    list.add(intent);
    List<String> listStr = [];
    for (var item in list) {
      listStr.add(item.toString());
    }
    areaMethodChannel
        .invokeMethod<String>('shareIntent', {'shareList': list.toString()});
  });

  // 测试 deleteIntent 方法
  test('deleteIntent test', () {
    areaMethodChannel
        .invokeMethod<String>('deleteIntent', {'intentName': 'PlayMusic'});
  });

  // 测试 deleteEntity 方法
  test('deleteEntity test', () {
    areaMethodChannel.invokeMethod<String>('deleteEntity', {
      'entityName': 'Music',
      'entityIds': ['C10194368']
    });
  });

  // 测试 deleteEntity 方法
  test('getSide test', () {
    areaMethodChannel.invokeMethod<String>(
        'getSide', {'renew': true}).then((value) => expect(value, isNotEmpty));
  });
}
